<template>
  <div>
    <el-form label-width="80px">
      <el-form-item label="spu名称"> 
        {{spu.spuName}}
      </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="sku名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" type="number" v-model="skuInfo.price"></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" rows="5" placeholder="规格描述" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="品台属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item :label="attr.attrName" v-for="attr in spuAttrInfoList" :key="attr.id">
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option :label="attrValue.valueName" :value="`${attr.id}:${attrValue.id}`" v-for="attrValue in attr.attrValueList" :key="attrValue.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item :label="saleAttr.saleAttrName" v-for="saleAttr in spuSaleAttrList" :key="saleAttr.id">
            <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValueId">
              <el-option :label="saleAttrValue.saleAttrValueName" :value="`${saleAttr.id}:${saleAttrValue.id}`" v-for="saleAttrValue in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table style="width: 100%" border :data="spuImageList"  @selection-change="handleSelectionChange">>
          <el-table-column type="selection" prop="prop" width="80">
          </el-table-column>
          <el-table-column prop="" label="图片" width="width">
            <template slot-scope="{row}">
              <img :src="row.imgUrl" alt="" style="width: 100px; height: 100px">
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="" label="操作" width="width">
            <template slot-scope="{row}">
              <el-button type="primary" size="mini" v-if="row.isDefault == 0" @click="changeDefault(row)">默认设置</el-button>
              <el-button size="mini" v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveSku">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //存储图片信息
      spuImageList: [],
      //存储销售属性
      spuSaleAttrList: [],
      //存储品台属性
      spuAttrInfoList: [],
      //收集sku数据字段
      skuInfo: {
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        price: 0,
        skuName: '',
        weight: '',
        skuDesc: '',
        skuImageList: [],
        skuDefaultImg: '',
        skuAttrValueList: [],
        skuSaleAttrValueList: []
      },
      spu: {},
      //收集图片数据
      imageList: []
    }
  },
  methods: {
    async getSkuData(category1Id, category2Id, row) {
      // console.log(row);
      //收集父组件给与的数据
      this.skuInfo.category3Id = row.category3Id
      this.skuInfo.spuId = row.id
      this.skuInfo.tmId = row.tmId
      this.spu = row
      //获取图片数据
      let spuImgRes = await this.$API.spu.reqSpuInageList(row.id)
      // console.log(spuImgRes);
      if(spuImgRes.code == 200) {
        let list = spuImgRes.data
        list.forEach(item => {
          item.isDefault = 0
        })
        this.spuImageList = list
      }
      //获取销售属性数据
      let spuSaleAttrRes = await this.$API.spu.reqSpuSaleAttrList(row.id)
      // console.log(spuSaleAttrRes);
      if(spuSaleAttrRes.code == 200) {
        this.spuSaleAttrList = spuSaleAttrRes.data
      }
      //获取品台属性数据
      let spuAttrInfoRes = await this.$API.spu.reqAttrInfoList(category1Id, category2Id, row.category3Id)
      // console.log(spuAttrInfoRes);
      if(spuAttrInfoRes.code == 200) {
        this.spuAttrInfoList = spuAttrInfoRes.data
      }
    },
    //table表格选项事件
    handleSelectionChange(params) {
      this.imageList = params
    },
    //点击设置默认和默认切换
    changeDefault(row) {
      this.spuImageList.forEach(item => {
        item.isDefault = 0
      })
      row.isDefault = 1
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    //保存
    async saveSku() {
      //整理参数
      const { spuAttrInfoList, skuInfo, spuSaleAttrList, imageList } = this
      //整理品台属性数据
      skuInfo.skuAttrValueList = spuAttrInfoList.reduce((prev,item) => {
        if(item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(':')
          prev.push({
            attrId,
            valueId
          })
        }
        return prev
      }, [])

      //整理销售属性数据
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev,item) => {
        if(item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(':')
          prev.push({
            saleAttrId,
            saleAttrValueId
          })
        }
        return prev
      }, [])

      //整理图片数据
      skuInfo.skuImageList = imageList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id
        }
      })

      //发请求
      // let res = await this.$API.spu.reqAddSku(skuInfo)
      // console.log(res);
      // if(res.code == 200) {
      //   this.$message({
      //     type:'success',
      //     message:'添加SKU成功'
      //   })
      // }
        this.$emit('changeScenes', 0)
    },
    //取消
    cancel() {
      this.$emit('changeScenes', 0)
      //清除数据
      Object.assign(this._data, this.$options.data())
    }
  },
};
</script>

<style>
</style>